<?php

//小站标签模块
class Tag_model extends Model {

	function Tag_model()
	{
		parent::Model();	
		$this->load->database();
	}
	
	//标签列表
	function lists()
	{
		$lists = array();
		$query = $this->db->from("site_tags")->order_by("id")->get();
		if ($query->num_rows()>0) {
			$lists = $query->result_array();
		}
		return $lists;
	}
	
	//修改标签
	function update_user($userid, $tags)
	{
		//删除该站标签
		$this->db->delete("accounts_tags_info", array("userid"=>$userid));
		//重新插入标签
		if ($tags != "") {
			$tags = explode(",", $tags);
			foreach ($tags as $tag) {
				if ($tag != "") {
					$data = array(
						"userid" => $userid,
						"tagid" => $tag,
						"addtime" => time(),
					);
					$this->db->insert("accounts_tags_info", $data);
				}
			}
		}
	}
	
	//修改标签
	function update_site($siteid, $tags)
	{
		//删除该站标签
		$this->db->delete("site_tags_info", array("siteid"=>$siteid));
		//重新插入标签
		if ($tags != "") {
			$tags = explode(",", $tags);
			foreach ($tags as $tag) {
				if ($tag != "") {
					$data = array(
						"siteid" => $siteid,
						"tagid" => $tag,
						"addtime" => time(),
					);
					$this->db->insert("site_tags_info", $data);
				}
			}
		}
	}
	
	//获取用户标签信息
	function get_user($userid)
	{
		$ids = array();
		$query = $this->db->get_where("accounts_tags_info", array("userid"=>$userid));
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row) {
				$ids[] = $row["tagid"];
			}
		}
		return $ids;
	}
	
	//获取用户标签信息
	function get_usertags($userid)
	{
		$tags = array();
//		$query = $this->db->get_where("accounts_tags_info", array("userid"=>$userid));
		$this->db->select("*");
		$this->db->from("accounts_tags_info");
		$this->db->where("accounts_tags_info.userid", $userid);
		$this->db->join("site_tags","site_tags.id = accounts_tags_info.tagid");
		$query = $this->db->get();
		if ($query->num_rows()>0) {
			$tags = $query->result_array();
		}
		return $tags;
	}
	
	//获取小站标签信息
	function get_site($siteid)
	{
		$ids = array();
		$query = $this->db->get_where("site_tags_info", array("siteid"=>$siteid));
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row) {
				$ids[] = $row["tagid"];
			}
		}
		return $ids;
	}
	
}

/* End of file */